package amap

import (
	"hft/Http/Services/front/amap"
	"hft/Http/Services/front/amap/amap_model"
	utils "hft/Http/Utils"
	map_tool "hft/tools/map_tool"

	"github.com/gin-gonic/gin"
)

type Amap struct{}

// 周边搜索
func (*Amap) SearchAround(c *gin.Context) {
	var data amap_model.SearchAround
	c.ShouldBind(&data)
	ip := c.ClientIP()
	if res, err := amap.AmapService.SearchAround(&data, ip); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.OkWithData(res, c)
	}
}

// 关键字
func (*Amap) KeyWordSearch(c *gin.Context) {
	var data amap_model.KeyWordSearch
	c.ShouldBind(&data)
	ip := c.ClientIP()
	if res, err := amap.AmapService.KeyWordSearch(&data, ip); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.OkWithData(res, c)
	}
}

func (*Amap) GetIp(c *gin.Context) {
	ip := c.ClientIP()
	if city, location, err := amap.AmapService.GetIp(ip); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.OkWithData(gin.H{
			"location": location,
			"city":     city,
		}, c)
	}
}

func (*Amap) GetIpV2(c *gin.Context) {
	ip := c.ClientIP()
	city := map_tool.AmapApi.GetIp(ip)
	utils.OkWithData(gin.H{"city": city}, c)
}

var AmapController = new(Amap)
